package com.mcflies.commons;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class McfliesCommonsUtil {
	/**
	 * @param menuArray
	 * @param assetManager
	 * @param imageDir
	 *            ：GridView中存放图片的文件夹名字
	 * @return
	 * @commont:根据传入的menuArray和图片文件夹imageDir
	 */
	public static List<Map<String, Object>> assembleMenuDataList(
			String[] menuArray, AssetManager assetManager, String imageDirName) {
		List<Map<String, Object>> menuDataList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < menuArray.length; i++) {
			String tempItemStr = menuArray[i];
			String[] tempItemArray = tempItemStr.split("@");
			if (tempItemArray.length != 3) {// 目前控制为三个
				continue;
			}

			Map<String, Object> tempMap = new HashMap<String, Object>();
			// 菜单标题
			String tempMenuDes = tempItemArray[0];
			tempMap.put(McfliesConstant.MenuMainConstant.MENU_MAIN_DES,
					tempMenuDes);

			// 菜单图片
			String tempMenuImageName = tempItemArray[1];
			if (!(tempMenuImageName.endsWith(".jpg")
					|| tempMenuImageName.endsWith(".png") || tempMenuImageName
						.endsWith(".ico"))) {// 只支持.jpg、.png和.ico格式的图片文件
				tempMenuImageName = "menuMain_default.png";
			}
			InputStream is = null;
			try {
				is = assetManager.open(imageDirName + File.separatorChar
						+ tempMenuImageName);
				if (null == is) {
					is = assetManager.open(imageDirName + File.separatorChar
							+ "menuMain_default.png");
				}
			} catch (Exception e) {
				Log.i(McfliesConstant.MCFLIES_EXCEPTION_INFO_FLAG,
						"McfliesCommonsUtil.java&&initMenuDataList()方法异常："
								+ e.getMessage());
			}
			Bitmap tempMenuImage = BitmapFactory.decodeStream(is);
			tempMap.put(McfliesConstant.MenuMainConstant.MENU_MAIN_IMAGE,
					tempMenuImage);
			// 点击菜单目标Activity
			String tempMenuIntent = tempItemArray[2];
			tempMap.put(McfliesConstant.MenuMainConstant.MENU_MAIN_INTENT,
					tempMenuIntent);
			menuDataList.add(tempMap);
		}
		return menuDataList;
	}
}
